package com.heima.article.listener;

import com.alibaba.fastjson.JSON;
import com.heima.article.dto.ArticleStreamMessage;
import com.heima.article.service.IApArticleService;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Service;

@Service
public class HotArticleResultListener {

    @Autowired
    private IApArticleService articleService;

    @KafkaListener(topics = "hot_article_result_topic")
    public void handleMessage(ConsumerRecord<String, String> record) {
        // 获取消息,转换称为结果对象 ArticleStreamMessage
        System.out.println("接收到消息: " + record.value());
        ArticleStreamMessage articleStreamMessage = JSON.parseObject(record.value(), ArticleStreamMessage.class);
        articleService.updateHotArticle(articleStreamMessage);
    }
}
